package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Attendance;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.AttendanceService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 请假/加班申请
     * @param attendance
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/add_attendance",method = RequestMethod.POST)
    public Result addAttendance(Attendance attendance){
        try {
            attendanceService.addAttendance(attendance);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 请假/加班删除
     * @param attendanceId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/delete_attendance")
    public Result deleteAttendance(Integer attendanceId){
        try {
            attendanceService.deleteAttendance(attendanceId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 请假/加班更新
     * @param attendance
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/update_attendance",method = RequestMethod.POST)
    public Result updateAttendance(Attendance attendance){
        try {
            attendanceService.updateAttendance(attendance);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 根据考勤ID查询考勤信息
     * @param attendanceId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/get_attendanceById")
    public Result getAttendanceById(Integer attendanceId){
        try {
            Attendance attendanceByAttendanceId = attendanceService.getAttendanceByAttendanceId(attendanceId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(attendanceByAttendanceId);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 分页查询列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/getAttendancePage")
    public Result getAttendancePage(Integer pageNo,Integer pageSize){
        try {
            Page attendancePage = attendanceService.getAttendancePage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(attendancePage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

}
